<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_services_CookieService extends f_core_Service
{
    private static $instance;

    private $session;

    private $userName;

    private $urls;

	/**
	 * @return m_backoffice_services_CookieService
	 */
	private function __construct($userName = null)
	{
        if(is_null($userName))
        {
            $user = f_core_Context::getInstance()->getContext('user');
            if(is_object($user))
            {
                $this->userName = $user->getUser()->getLogin();
            }
        }
        
        $this->session = f_core_Session::getInstance();
        $this->initValuesFromSession();
		return $this;
	}

	/**
	 * Get singleton instance
	 *
	 * @return m_backoffice_services_CookieService
	 */
	public static function getInstance($userName = null)
	{       
		if(self::$instance === null)
		{
			self::$instance = new self($userName);
		}
		return self::$instance;
	}

    private function initValuesFromSession()
    {
        $session = $this->session;

        $this->urls['lastUrl'] = $session->getValue('lastUrl', f_core_Routing::getUrl('websiteListWoId'));
        $this->urls['modulewebsiteurl'] = $session->getValue('modulewebsiteurl', f_core_Routing::getUrl('websiteListWoId'));
        $this->urls['modulemediasurl'] = $session->getValue('modulemediasurl', f_core_Routing::getUrl('mediasRoot'));
        $this->urls['moduleusersurl'] = $session->getValue('moduleusersurl', f_core_Routing::getUrl('usersList'));
        $this->urls['modulemodulesurl'] = $session->getValue('modulemodulesurl', f_core_Routing::getUrl('modulesRoot'));
        $this->urls['modulesystemurl'] = $session->getValue('modulesystemurl', f_core_Routing::getUrl('systemRoot'));
    }

    public function setLastUrl($url)
    {
        $this->urls['lastUrl'] = $url;
        return $this;
    }

    public function getLastUrl()
    {
        return $this->urls['lastUrl'];
    }

    public function setMenuUrl($category, $url)
    {
        $category = strtolower($category);

        $categories = array('website', 'medias', 'users');

        if(!in_array($category, $categories))
        {
            $category = 'modules';
        }

        if(!is_null(f_core_Request::getInstance()->getParameter("panel")))
        {
            $category = 'system';
        }

        $this->urls['module'.$category.'url'] = $url;
        $this->session->setValue('module'.$category.'url', $url);
        return $this;
    }

    public function getMenuUrl($category)
    {
        return $this->urls['module'.strtolower($category).'url'];
    }

    public function save()
    {
        /* @var $session f_core_Session */
        $session = $this->session;
        
        $session->setValue('lastUrl', $this->urls['lastUrl']);
        $session->setValue('modulewebsiteurl', $this->urls['modulewebsiteurl']);
        $session->setValue('modulemediasurl', $this->urls['modulemediasurl']);
        $session->setValue('moduleusersurl', $this->urls['moduleusersurl']);
        $session->setValue('modulemodulesurl', $this->urls['modulemodulesurl']);
        $session->setValue('modulesystemurl', $this->urls['modulesystemurl']);

        f_core_Cookie::getInstance()->setParameters($this->userName, time()+2592000)->setCookie($this->urls);
    }
}
